package cn.itcast.web.action;

import cn.itcast.domain.delivery.WayBill;
import cn.itcast.service.WayBillService;
import cn.itcast.utils.ResponseUtils;
import cn.itcast.web.action.common.BaseAction;
import com.alibaba.fastjson.JSONArray;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

@Namespace("/")
@ParentPackage("struts-default")
@Controller
@Scope("prototype")
@Actions
public class WayBillAction extends BaseAction<WayBill> {
    @Autowired
    private WayBillService wayBillService;

    //$.post("../../waybill_save.action",rowData,function (data)
    @Action(value = "waybill_save")
    public String save() {
        //保存model  响应保存结果
        Map<String, Object> result = new HashMap<>();
        try {
            //去除model关联的无id的order对象
            if(model.getOrder()!=null&&(model.getOrder().getId()==null||model.getOrder().getId()==0)){
                model.setOrder(null);
            }
            wayBillService.save(model);
            //保存成功
            result.put("success", true);
            result.put("msg", "数据保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            //保存失败
            result.put("success", false);
            result.put("msg", "数据保存失败");
        }
        //响应数据到页面
        String data = JSONArray.toJSONString(result);
        ResponseUtils.responseWriter(data);
        return NONE;
    }

    //url :  "../../waybill_pageQuery.action", 分页查询
     //页面数据查询优化从索引库查询数据
    @Action(value = "waybill_pageQuery")
    public String pageQuery() {
        Pageable pageable = new PageRequest(page - 1, rows, new Sort(new Sort.Order(Sort.Direction.DESC, "id")));
        Page<WayBill> pageData = wayBillService.findPageData(model,pageable);
        Map<String, Object> result = new HashMap<>();
        result.put("total", pageData.getTotalElements());
        result.put("rows", pageData.getContent());
        String data = JSONArray.toJSONString(result);
        ResponseUtils.responseWriter(data);
        return NONE;
    }

    //$.post("../../waybill_findByWayBillNum.action数据反显查询
    @Action(value = "waybill_findByWayBillNum")
    public String findByWayBillNum() {
        Map<String, Object> result = new HashMap<>();
        WayBill waybill = wayBillService.findByWayBillNum(model.getWayBillNum());
        if (waybill == null) {
            result.put("success", false);
        } else {
            result.put("success", true);
            result.put("waybillData", waybill);
        }
        String data = JSONArray.toJSONString(result);
        ResponseUtils.responseWriter(data);
        return NONE;
    }
}
